#define _CRT_SECURE_NO_WARNINGS

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <impl.h>


extern "C" int LLVMFuzzerTestOneInput(const char *data, size_t size) {

    // for (int i = 1; i % 2; i += 2) {
    // }

    /// atoi:
    /// libfuzer + asan, has bug.
    ///
    /// ==11300==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x12194f5a0a71 at pc 0x7ff76ce0b618 bp 0x00c4fc4fe660 sp 0x00c4fc4fe6a0
    /// READ of size 2 at 0x12194f5a0a71 thread T0
    ///     #0 0x7ff76ce0b617 in __asan_wrap_atoi C:\src\llvm_package_16.0.5\llvm-project\compiler-rt\lib\asan\asan_interceptors.cpp:541
    ///     #1 0x7ff76cdf16f6 in LLVMFuzzerTestOneInput X:\_fuzz_targets\jc_first_fuzz_sample\jc_first_fuzz_sample\src\main.libfuzzer.c:96

    if (size > 0 && data[0] == '0' || size > 9) {
        return 0;
    }

#if 1
    int digits = 0;
    for (size_t i = 0; i < size; i++) {
        auto ch = data[i];
        if (ch < 0x30 || ch > 0x39) {
            if (size == 5) {
                digits = 0;
                break;
            } else {
                return 0;
            }
        }
        digits = digits * 10 + (ch - 0x30);
    }
#else
    int digits = atoi(data);
#endif

    harness_inner(data, size, digits);\

    return 0;
}
